// pages/review/reviewStart/reviewStart.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        table:0,
        question: [null,null,null,null],
        state: true ,
        wrongShow: false,
        wrongWord: null,
        wrongTranslation: null
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.setData({
            table:options.table
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        this.getNextReview()
    },
    checkAnswer(e){
            var app = getApp()
            var word;
            var translation;
            for(let i in this.data.question){
                var item = this.data.question[i]
                if(item.id != 0){
                    word = item.word;
                    translation = item.translation;
                    break
                }
            }
        if(e.detail.result){
            wx.request({
              url: 'http://39.101.199.5:8888/SuJiWords/Review/updateReview',
              method:"POST",
              data:{
                "userId": app.globalData.userId,
                "wordTable":this.data.table,
                "word": word,
                "translation":translation,
                "master":1
              },
              success:res=>{
                  console.log(res)
                  this.getNextReview()
              },
              fail:res =>{
                this.setData({
                    question: [null,null,null,null],
                    state: true ,
                    wrongShow: false,
                    wrongWord: null,
                    wrongTranslation: null
                    })
              }
            })
        }else{
            this.setData({
                wrongWord: word,
                wrongTranslation: translation,
                wrongShow: true
            })

            wx.request({
                url: 'http://39.101.199.5:8888/SuJiWords/Review/updateReview',
                method:"POST",
                data:{
                  "userId": app.globalData.userId,
                  "wordTable":this.data.table,
                  "word": word,
                  "translation":translation,
                  "master":0
                },
                success:res=>{
                    console.log(res)
                    this.getNextReview()
                },
                fail:res =>{
                    this.setData({
                        question: [null,null,null,null],
                        state: true ,
                        wrongShow: false,
                        wrongWord: null,
                        wrongTranslation: null
                        })
                  }
              })
            console.log("错误")
        }
    },
    getNextReview(){
        var app = getApp();
        
        wx.request({
          url: 'http://39.101.199.5:8888/SuJiWords/Review/getReviewWord',
          method:"POST",
          data:{
            "phoneNumber":app.globalData.loginPhoneNumber,
            "table":this.data.table
          },
          success: res =>{
              var data = res.data.data;
              var arr = [data.answer,data.wrongEnwords[0],data.wrongEnwords[1],data.wrongEnwords[2]];
              for(let i = 0;i < arr.length;i++){
                  var num =Math.floor(Math.random()*4) 
                  var a = arr[i];
                  arr[i] = arr[num];
                  arr[num] = a;
              }
              
            this.setData({
                question:arr,
                state:Math.random()>0.5?true:false
            })
        
          }
        })
    }


})